home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / tex_cube / oglwindow.c++ < prev    next >
C/C++ Source or Header  |  1996-11-11  |  6KB  |  244 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include "oglwindow.h"
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42.  
  43. static int RGBA_SB_attributes[] = {
  44.   GLX_RGBA,
  45.   GLX_RED_SIZE, 1,
  46.   GLX_GREEN_SIZE, 1,
  47.   GLX_BLUE_SIZE, 1,
  48.   GLX_DEPTH_SIZE, 1,
  49.   None,
  50. };
  51.  
  52. static int RGBA_DB_attributes[] = {
  53.   GLX_RGBA,
  54.   GLX_RED_SIZE, 1,
  55.   GLX_GREEN_SIZE, 1,
  56.   GLX_BLUE_SIZE, 1,
  57.   GLX_DOUBLEBUFFER,
  58.   GLX_DEPTH_SIZE, 1,
  59.   None,
  60. };
  61.  
  62. oglwindow::oglwindow()
  63. {
  64.   doublebuffer = 1;
  65.   minx = miny = maxx = maxy = 0;
  66.   event_mask = ExposureMask | StructureNotifyMask | KeyPressMask |
  67.     ButtonPressMask;
  68.   strncpy(title, "OpenGL", max_titlelength);
  69. }
  70.  
  71. void oglwindow::set_doublebuffer(int new_doublebuffer)
  72. {
  73.   doublebuffer = new_doublebuffer;
  74. }
  75.  
  76. void oglwindow::set_minsize(int new_minx, int new_miny) 
  77. {
  78.   minx = new_minx;
  79.   miny = new_miny;
  80. }
  81.  
  82. void oglwindow::set_maxsize(int new_maxx, int new_maxy)
  83. {
  84.   maxx = new_maxx;
  85.   maxy = new_maxy;
  86. }
  87.  
  88. void oglwindow::set_title(char *new_title)
  89. {
  90.   strncpy(title, new_title, max_titlelength);
  91. }
  92.  
  93. void oglwindow::set_event_mask(unsigned long new_event_mask) 
  94. {
  95.   event_mask = new_event_mask;
  96. }
  97.  
  98. void oglwindow::add_event_mask(unsigned long new_flag)
  99. {
  100.   event_mask |= new_flag;
  101. }
  102.  
  103. unsigned long oglwindow::get_event_mask()
  104. {
  105.   return event_mask;
  106. }
  107.  
  108. int oglwindow::get_width() 
  109. {
  110.   XWindowAttributes winattr;
  111.   XGetWindowAttributes(dpy, window, &winattr);
  112.   return winattr.width;
  113. }
  114.  
  115. int oglwindow::get_height() 
  116. {
  117.   XWindowAttributes winattr;
  118.   XGetWindowAttributes(dpy, window, &winattr);
  119.   return winattr.height;
  120. }
  121.  
  122. char *oglwindow::get_title()
  123. {
  124.   return title;
  125. }
  126.  
  127. void oglwindow::open() 
  128. {
  129.   int can_ogl, can_zbuffer, can_rgba;
  130.   XSetWindowAttributes swa;
  131.   XSizeHints hints;
  132.  
  133.   dpy = XOpenDisplay(0);
  134.   if (dpy == NULL) {
  135.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  136.     exit(1);
  137.   }
  138.  
  139.   vi = glXChooseVisual(dpy, DefaultScreen(dpy), 
  140.                doublebuffer ? RGBA_DB_attributes : 
  141.                RGBA_SB_attributes);
  142.   if (vi == NULL) {
  143.     fprintf(stderr, "No matching visual on \"%s\"\n",
  144.         getenv("DISPLAY"));
  145.     exit(1);
  146.   }
  147.  
  148.   glXGetConfig(dpy, vi, GLX_USE_GL, &can_ogl);
  149.   if (!can_ogl) {
  150.     system("inform 'Your system must support OpenGL to run this program'");
  151.     exit(1);
  152.   }
  153.   glXGetConfig(dpy, vi, GLX_RGBA, &can_rgba);
  154.   if (!can_rgba) {
  155.     system("inform 'Your system must support RGB mode to run this program'");
  156.     exit(1);
  157.   }
  158.   glXGetConfig(dpy, vi, GLX_DEPTH_SIZE, &can_zbuffer);
  159.   if (can_zbuffer == 0) {
  160.     system("inform 'Your system must have a z-buffer to run this program'");
  161.     exit(1);
  162.   }
  163.  
  164.   swa.border_pixel = 0;
  165.   swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  166.                  vi->visual, AllocNone);
  167.   swa.event_mask = event_mask;
  168.   window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10, 50, 50,
  169.              0, vi->depth, InputOutput, vi->visual,
  170.              CWBorderPixel | CWColormap | CWEventMask, &swa);
  171.   XStoreName(dpy, window, title);
  172.  
  173.   hints.flags = 0;
  174.   if (minx && miny) {
  175.     hints.min_width = minx;
  176.     hints.min_height = miny;
  177.     hints.flags |= PMinSize;
  178.   }
  179.   if (maxx && maxy) {
  180.     hints.max_width = maxx;
  181.     hints.max_height = maxy;
  182.     hints.flags |= PMaxSize;
  183.   } 
  184.   if (!hints.flags) {
  185.     hints.min_aspect.x = hints.min_aspect.y =
  186.       hints.max_aspect.x = hints.max_aspect.y = 1;
  187.     hints.flags = PAspect;
  188.   }
  189.   XSetNormalHints(dpy, window, &hints);
  190.  
  191. }
  192.  
  193. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  194. {
  195.   if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  196.     return GL_TRUE;
  197.   }
  198.   return GL_FALSE;
  199. }
  200.  
  201. void oglwindow::map() 
  202. {
  203.   XEvent ev;
  204.  
  205.   XMapWindow(dpy, window);
  206.   XIfEvent(dpy, &ev, WaitForMapNotify, (char *)window);
  207.  
  208.   ctx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  209. }
  210.  
  211. void oglwindow::winset()
  212. {
  213.   glXMakeCurrent(dpy, window, ctx);
  214. }
  215.  
  216. void oglwindow::swapbuffers()
  217. {
  218.   glXSwapBuffers(dpy, window);
  219. }
  220.  
  221. void oglwindow::resize() 
  222. {
  223.   XWindowAttributes windowattr;
  224.  
  225.   XGetWindowAttributes(dpy, window, &windowattr);
  226.   glViewport(0, 0, windowattr.width, windowattr.height);
  227. }
  228.  
  229. Display *oglwindow::get_display() 
  230. {
  231.   return dpy;
  232. }
  233.  
  234. Window oglwindow::get_window()
  235. {
  236.   return window;
  237. }
  238.  
  239. int oglwindow::get_doublebuffer() 
  240. {
  241.   return doublebuffer;
  242. }
  243.  
  244.